//
//  YWEquipmentMoreDetailTableViewController.m
//  maintain
//
//  Created by 郑想 on 16/6/6.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWEquipmentMoreDetailTableViewController.h"
#import "YWMacro.h"
#import "UIColor+CustomColors.h"

#import "YWBuildingAndLocationCell.h"
#import "YWEquUseMethodAndRangeTableViewController.h"
#import "YWEquInitParameterTableViewController.h"
#import "YWMeterReadingManager.h"

@implementation YWEquipmentMoreDetailTableViewController

- (instancetype)init{
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设备详情";
    self.view.backgroundColor = [UIColor customBackgroudColor];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.equipmentDataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *countArray = self.titleDataSource[section];
    return countArray.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    switch (section) {
        case 0:
            return @"设备位置";
            break;
        case 1:
            return @"设备信息";
            break;
        default:
            return @"";
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:
            return kLabelH * 2 + kMarginW * 2;
            break;
        default:
            return kLabelH * 1 + kMarginW * 2;
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return 35;
            break;
        case 1:
            return 35;
            break;
        case 2:
            return 12;
            break; 
        default:
            return 0;
            break;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case 0:{
            YWBuildingAndLocationCell* cell = [YWBuildingAndLocationCell cellWithTableView:tableView];
            NSArray *entity = self.equipmentDataSource[indexPath.section];
            cell.bulidingNameLabel.text = entity.count ? entity[0] : @"无";
            cell.bulidingLocationLabel.text = entity.count>1 ? entity[1] : @"无";
            return cell;
        }
            break;
        default:{
            UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"equDetailCell"];
            if (cell==nil) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"equDetailCell"];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            NSString *title = self.titleDataSource[indexPath.section][indexPath.row];
            NSString *content = self.equipmentDataSource[indexPath.section][indexPath.row];
            cell.textLabel.text = title;
            cell.detailTextLabel.text = content;
            if (indexPath.section==1 && ([title isEqualToString:@"初始参数"] || [title isEqualToString:@"使用方法和范围"])) {
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            }else{
                cell.accessoryType = UITableViewCellAccessoryNone;
            }
            
            return cell;
        }
            break;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *title = self.titleDataSource[indexPath.section][indexPath.row];
    if (indexPath.section == 1 && [title isEqualToString:@"初始参数"]) {
        YWEquInitParameterTableViewController *initParameterVC = [[YWEquInitParameterTableViewController alloc] init];
        initParameterVC.equipmentId = _equipmentId;
        [self.navigationController pushViewController:initParameterVC animated:YES];
    }else if (indexPath.section == 1 && [title isEqualToString:@"使用方法和范围"]){
        YWEquUseMethodAndRangeTableViewController *useMethodAndRangeVC = [[YWEquUseMethodAndRangeTableViewController alloc] init];
        useMethodAndRangeVC.equipmentTypeId = _equipmentTypeId;
        [self.navigationController pushViewController:useMethodAndRangeVC animated:YES];
    }
}

@end
